package com.zhika.zishi.empower;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.exception.WeiboException;
import com.zhika.zishi.fragments.login.EntryFragment;

/**
 * Created by Michael on 2015/4/6.
 */
public class SinaAuthListener implements WeiboAuthListener {

    private Oauth2AccessToken accessToken;

    private Context context;
    public SinaAuthListener(Context context){
        this.context = context;
    }

    @Override
    public void onComplete(Bundle bundle) {

        //从bundle中解析token
        accessToken = Oauth2AccessToken.parseAccessToken(bundle);
        if (accessToken.isSessionValid()) {

            //这边得获取微博用户的个人信息，调微博的接口，网络请求
            String token = accessToken.getToken();
            String userId = accessToken.getUid();

//            LogUtil.e("token=", token + ";userId = " + userId);
            Intent intent = new Intent(context, EntryFragment.class);
            context.startActivity(intent);

        } else {
            String code = bundle.getString("code");
            String message = "授权失败";
            if (!TextUtils.isEmpty(code)) {
                message = message + "\nObtained the code:" + code;
            }
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onCancel() {
        Toast.makeText(context, "取消授权", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWeiboException(WeiboException e) {
        Toast.makeText(context, "Auth exception :" + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
